package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ListActor {
	int lacid;
	int fid;
	int acid;
	public ListActor(){};
	public ListActor(int lacid, int fid, int acid) {
		this.lacid = lacid;
		this.fid = fid;
		this.acid = acid;
	}
	public int getLacid() {
		return lacid;
	}
	public void setLacid(int lacid) {
		this.lacid = lacid;
	}
	public int getFid() {
		return fid;
	}
	public void setFid(int fid) {
		this.fid = fid;
	}
	public int getAcid() {
		return acid;
	}
	public void setAcid(int acid) {
		this.acid = acid;
	}
	public boolean Create() {
		PreparedStatement sqlr = null;
		boolean result = false;

		Connection con = new ConnectDB().getConnection();
		try {
			sqlr = con
					.prepareStatement("INSERT INTO listactor( fid, acid) VALUES (? , ?)");
			
			sqlr.setInt(1, this.fid);
			sqlr.setInt(2, this.acid);
			int number = sqlr.executeUpdate();
			if (number > 0) {
				result = true;
			}
			sqlr.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	public boolean del(int fid, int acid){
		PreparedStatement delpd = null;
		Connection con = new ConnectDB().getConnection();
		try {
			delpd = con.prepareStatement("DELETE FROM listactor WHERE fid = ? AND acid = ?  ");
			delpd.setInt(1, fid);
			delpd.setInt(2, acid);
			int number = delpd.executeUpdate();
			if (number > 0) {
				return true;
			}
			delpd.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return  false;
	}
	public List<ListActor> getListAcTorByFid(int fid ){
		List<ListActor> result = new ArrayList<ListActor>();
		PreparedStatement sqlp = null;
		Connection connection = new ConnectDB().getConnection();
		try {
			sqlp = connection.prepareStatement("SELECT * FROM listactor where fid= ? ");
			sqlp.setInt(1, fid);
			ResultSet rs = sqlp.executeQuery();
			while (rs.next() == true) {
				int lacid = rs.getInt(1);
				int f = rs.getInt(2);
				int a = rs.getInt(3);
				result.add(new ListActor(lacid, f, a));
			}
			sqlp.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	public List<ListActor> getListAcTorByAcid(int acid ){
		List<ListActor> result = null;
		PreparedStatement sqlp = null;
		Connection connection = new ConnectDB().getConnection();
		try {
			sqlp = connection.prepareStatement("SELECT * FROM listactor where acid= ? ");
			sqlp.setInt(1, acid);
			ResultSet rs = sqlp.executeQuery();
			while (rs.next() == true) {
				int lacid = rs.getInt(1);
				int f = rs.getInt(2);
				int a = rs.getInt(3);
				result.add(new ListActor(lacid, f, a));
			}
			sqlp.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
}
